jQuery(document).ready(function(){
	jQuery("ul.nav-right li.login a").colorbox({
		onComplete: function(){
			jQuery("#bt_login").click(function(){
				jQuery('#login-error-message').html('');
				params = getParams('login_form');
				jQuery.post(SITE_URL + 'account/login/do_login', params, function(data){
					if(data.result){
						jQuery.colorbox.close();
						window.location.href = data.url;
					}else{
						jQuery('#login-error-message').html(data.message);
					}
				}, 'json');
				return false;
			});
			
		}
	});
	jQuery('#upload_bt').click(function(){
		jQuery.colorbox({
			href: jQuery(this).attr('href'),
			innerWidth: 700,
			innerHeight: 400,
			initialWidth: 700,
			initialHeight: 400,
			open: true,
			onClosed: function(){
				if(jQuery('#list_folder').find('a.selected').length){
					jQuery('#list_folder').find('a.selected').click();
				}else{
					jQuery('#show_file table.data-table tbody').load(SITE_URL + 'file_manager/list_file/0');
				}
			}
		});
	});
	jQuery('#withdrawal').click(function(){
		jQuery.colorbox({
			href: SITE_URL + 'withdrawals',
			innerWidth: 700,
			innerHeight: 400,
			initialWidth: 700,
			initialHeight: 400,
			open: true,
			onCompleted: function(){
			
			}
		});
		return false;
	});
	jQuery('#withdrawal-request').click(function(){
		jQuery.colorbox({
			href: SITE_URL + 'withdrawals/request',
			innerWidth: 550,
			innerHeight: 250,
			initialWidth: 550,
			initialHeight: 250,
			open: true,
			onComplete: function(){
				jQuery('#withdrawal_request_cancel').click(function(){
					jQuery.colorbox.close();
				});
				jQuery('#withdrawal_request_submit').click(function(){
					jQuery('#uploking_box_error').html('');
					jQuery.post(SITE_URL + 'withdrawals/request', {request: 'request', money: jQuery('#money').val(), paypal_email:  jQuery('#paypal_email').val(), pin_number: jQuery('#pin_number').val()}, function(data){
						if(data.result){
							jQuery('#uploking_box_content').html('<div class="withdrawal-request-success">' + data.message + '</div>')
						}else{
							jQuery('#uploking_box_error').html(data.message);
						}
					}, 'json');
					return false;
				});
			}
		});
		return false;
	});
	jQuery('#buy_uploking').click(function(){
		buy_uploking();
		return false;
	});
	jQuery('.login_form').click(function(){
		do_login();
		return false;
	});
	jQuery('#login_form_link').click(function(){
		do_login();
		return false;
	});
	
	jQuery('.buy_package').click(function(){
		buy_uploking();
		return false;
	});
	jQuery('#upload_file_link').click(function(){
		window.location.href = SITE_URL + 'file_manager';
	});
});

function do_login(){
	jQuery.colorbox({
		href: jQuery("ul.nav-right li.login a").attr('href'),
		open: true,
		onComplete: function(){
			jQuery("#bt_login").click(function(){
				jQuery('#login-error-message').html('');
				params = getParams('login_form');
				jQuery.post(SITE_URL + 'account/login/do_login', params, function(data){
					if(data.result){
						jQuery.colorbox.close();
						window.location.href = data.url;
					}else{
						jQuery('#login-error-message').html(data.message);
					}
				}, 'json');
				return false;
			});
		}
	});
}

function buy_uploking(){
	jQuery.colorbox({
		href: SITE_URL + 'account/buy',
		innerWidth: 1050,
		innerHeight: 350,
		initialWidth: 1050,
		initialHeight: 350,
		open: true,
		onComplete: function(){
			jQuery('#buy_package_cancel').click(function(){
				jQuery.colorbox.close()
				return false;
			});
			jQuery('#buy_package_submit').click(function(){
				jQuery.post(SITE_URL + 'account/buy', {buy_package_request: 'buy_package_request', package: jQuery('input[name=selected_package]:checked').val()}, function(data){
					if(data.result){
						jQuery('#uploking_package_wrapper').html(data.message);
					}else{
						jQuery('#uploking_box_error').html(data.message);
					}
				}, 'json');
			});
		}
		
	});
}

function getParams(formId){
	var params = {};
	jQuery("#"+formId).find("input[checked], input[type='text'], input[type='hidden'], input[type='password'], input[type='submit'], option[selected], textarea").each(function() {
		params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value; 
	});
	return params;
}